/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;

import beans.AfiliadoSesionBean;
import beans.ObraSocialBean;
import daos.Dao;
import daos.DaoFactory;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import wso.AfiliadoSerializable;

/**
 *
 * @author C
 */
public class AfiliadoLoginAction extends Action{

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) {
        
        Class serviceClass, portClass;
        Object service, port;
        Method metGetPort, metGetLogin;       
        String os= request.getParameter("nom_os");
        
        try {
            Dao dao = DaoFactory.getDao("ObraSocial");             
            ObraSocialBean osBean=  (ObraSocialBean) dao.get(new ObraSocialBean(os));       
            
            serviceClass = Class.forName("wso."+osBean.getAccesoWS()+"_Service");
            portClass = Class.forName("wso." +osBean.getAccesoWS());
            service = serviceClass.newInstance();     
                        
            metGetPort    = serviceClass.getMethod("get"+osBean.getAccesoWS()+"Port", (Class[]) null);                
            Class[] cArg = new Class[1];
            cArg[0] = AfiliadoSerializable.class;                  
            port   = (Object) metGetPort.invoke(service, (Object[]) null);
            
            metGetLogin = portClass.getMethod("login", cArg);
                
            AfiliadoSerializable as= new AfiliadoSerializable();
            String nroAf=request.getParameter("user");
            as.setNroAfiliado(Long.parseLong(nroAf));
            as.setTipoDocumento(request.getParameter("tipo_doc"));
            as.setNroDocumento(Integer.parseInt(request.getParameter("doc")));
            Date d = new Date();
            SimpleDateFormat formateador = new SimpleDateFormat("yyyy-MM-dd");
            d = formateador.parse(request.getParameter("fechan"));
            GregorianCalendar g= new GregorianCalendar();
            g.setTime(d);
            XMLGregorianCalendar g2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(g);
            as.setFechaNacimiento(g2);
           
            as = (AfiliadoSerializable) metGetLogin.invoke(port, as);       
            
            AfiliadoSesionBean asb = new AfiliadoSesionBean(as.getNroAfiliado(),osBean.getId_os());
            dao = DaoFactory.getDao("AfiliadoSesion");
            long idSession = dao.insert(asb);          

            HttpSession session = request.getSession(true);           
            session.setAttribute("nomAfiliado", as.getNombre());
            session.setAttribute("apeAfiliado", as.getApellido());
            session.setAttribute("nom_os", os);
            session.setAttribute("nro_os", osBean.getId_os());
            session.setAttribute("user", as.getNroAfiliado());
            session.setAttribute("nroSesion", idSession);        
            
        } catch (Exception ex) { 
            request.setAttribute("Error", ex.getMessage());
            System.out.println("Error    " + ex.getMessage());
        }
        finally {
            this.gotoPage("/principal.jsp", request, response);
        }
    }

 

   

  



}